package com.jet.chat.command;

import java.util.regex.Matcher;

import com.jet.chat.dbquery.ChatQueryViewUnread;
import com.jet.chat.dbquery.ChatQueryViewUnreadParams;
import com.jet.chat.dbquery.ChatQueryViewUnreadResult;
import com.jet.chat.dbquery.ChatQueryViewUnreadResultRaw;
import com.jet.chat.message.Message;
import com.jet.chat.server.ChatServer;
import com.jet.chat.server.ChatSession;

public class ChatCommandViewUnread extends ChatCommandAbstract {

	public ChatCommandViewUnread() {
		super("^/\\w+$");
	}

	@Override
	protected void executeCommand(Matcher matcher, ChatServer chatServer, ChatSession chatSession, Message chatMessage) throws Exception {
		ChatQueryViewUnreadResult result = (ChatQueryViewUnreadResult) new ChatQueryViewUnread().execute(chatServer.getDBConnectionService(), new ChatQueryViewUnreadParams(chatSession.getUserId()));
		String content;
		Message message;
		for (ChatQueryViewUnreadResultRaw raw : result.getResult()) {
			if (raw.getPub() == 0) {
				content = raw.getTimestamp() + "\t" + raw.getSenderUsername() + " whispers: " + raw.getContent();
			} else {
				content = raw.getTimestamp() + "\t" + raw.getChannelName() + " : " + raw.getSenderUsername() + " > " + raw.getContent();
			}
			
			message = new Message(content);
			message.setId(raw.getMessageId());
			
			chatSession.getQueue().addMessage(message);
		}
	}

}
